package com.lslx.ln.sample.api.provider;


import com.lslx.ln.sample.DemoService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;

import java.util.concurrent.CountDownLatch;

/**
 * <pre>
 *  TODO
 * </pre>
 *
 * @Author zhangj
 * @Date 2022/6/26 18:24
 */
public class Application {

    public static void main(String[] args) throws InterruptedException {
        if(isClassic(args)) {
            startWithPort();
        } else {
            startWithBootstrap();
        }
    }

    private static void startWithBootstrap() {
        ServiceConfig<DemoServiceApiImpl> serverConfig = new ServiceConfig();
        serverConfig.setInterface(DemoService.class);
        serverConfig.setRef(new DemoServiceApiImpl());

        DubboBootstrap bootstrap = DubboBootstrap.getInstance();
        bootstrap.application(new ApplicationConfig("dubbo-demo-api-provider"))
                .registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
                .service(serverConfig)
                .start()
                .await();
    }

    private static void startWithPort() throws InterruptedException {
        ServiceConfig<DemoServiceApiImpl> serverConfig = new ServiceConfig();
        serverConfig.setInterface(DemoService.class);
        serverConfig.setRef(new DemoServiceApiImpl());
        serverConfig.setApplication(new ApplicationConfig("dubbo-demo-api-provider"));
        serverConfig.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        serverConfig.export();

        System.out.println("dubbo service started");
        new CountDownLatch(1).await();
    }

    private static boolean isClassic(String[] args) {
        return args.length > 0 && "classic".equalsIgnoreCase(args[0]);
    }



}
